import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';

@Injectable()
export class DbService {
  constructor(
    @InjectModel('fraction') private readonly FractionModel,
  ) { }

  async create(createDto): Promise<any> {
    const query = { dept: createDto.dept, date: createDto.date };
    const updates = { userInfo: createDto.userInfo };
    const options = { upsert: true, new: true, setDefaultsOnInsert: true };
    await this.FractionModel.findOneAndUpdate(query, updates, options, (err, raw) => {
      if (err) return;
      console.log('success');
    });
    return;
  }

  async find(params) {
    const {dept,date} = params;
    const userName = params.name?{"userInfo":{$elemMatch:{"name":params.name}}}:'';
    return await this.FractionModel.find({dept,date},userName).exec();
  }
}